package com.ruoyi.web.controller.resturant;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.ResDishes.domain.ResDishes;
import com.ruoyi.ResDishes.service.ResDishesService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Anonymous
@RestController
@RequestMapping("/api/resturant/dishes")
@Api(tags="菜品管理")
public class ResDishesController extends BaseController {

    @Autowired
    private ResDishesService resDishesService;

    @ApiOperation("查询全部菜品 if")
    @PostMapping("/listIf")
    //2617王乙媞
    public AjaxResult listif(@RequestBody ResDishes resDishes) {
        List<ResDishes> list = resDishesService.selectResDishesByParamIf(resDishes);
        return AjaxResult.success(list);
    }

    @ApiOperation("查询全部菜品 when")
    @PostMapping("/listWhen")
    //2617王乙媞
    public AjaxResult listwhen(@RequestBody ResDishes resDishes) {
        List<ResDishes> list = resDishesService.selectResDishesByParamWhen(resDishes);
        return AjaxResult.success(list);
    }

    /**
     * 获取菜品详细信息
     */
    @Autowired
    RedisCache cache;
    @ApiOperation(value = "获取菜品详细信息", notes = "TODO：取得某个菜品的详细信息")
    @ApiImplicitParam(name = "id", value = "菜品ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")

    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ResDishes resDishes=resDishesService.selectResDishesById(id);
        String json= JSON.toJSONString(resDishes);
        cache.setCacheObject(String.valueOf(id),json,2000, TimeUnit.SECONDS);
        return AjaxResult.success(resDishes);
    }


    /**
     * 新增菜品
     */
    @ApiOperation("新增菜品")
    @PostMapping
    //2617王乙媞
    public AjaxResult add(@RequestBody ResDishes resDishes) {
        return AjaxResult.success(resDishesService.insertResDishes(resDishes));
    }


    /**
     * 修改菜品
     */
    @ApiOperation("修改菜品")
    @PutMapping
    //2617王乙媞
    public AjaxResult edit(@RequestBody ResDishes resDishes) {
        return AjaxResult.success(resDishesService.updateResDishes(resDishes));
    }


    /**
     * 删除菜品
     */
    @ApiOperation("删除菜品")
    @ApiImplicitParam(name = "id", value = "菜品ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    //2617王乙媞
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(resDishesService.deleteResDishesById(id));
    }
}
